package sitemaps;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class StringLoader {
	
	private String encoding;

	public StringLoader(String encoding) {
		this.encoding = encoding;
	}

	public String load(InputStream is) throws IOException {
		BufferedInputStream bis = null;
		ByteArrayOutputStream os = null;
		try { 
			os = new ByteArrayOutputStream();
			bis = new BufferedInputStream(is);
			transfer(bis, os);
			return os.toString(encoding);
		} finally {
			try {
				if(is != null) is.close();
			} finally {
				if(os != null) os.close();
			}
		}

	}

	private void transfer(InputStream in, OutputStream out) throws IOException {
		int br;
		while((br = in.read()) != -1) {
			out.write(br);
		}
	}

}
